package edu.webtech.project.server;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import edu.webtech.project.client.UserAccountService;
import edu.webtech.project.server.domain.UserAccount;
import edu.webtech.project.shared.UserInfoDTO;

@SuppressWarnings("serial")
public class UserAccountServiceImpl extends RemoteServiceServlet implements
		UserAccountService {

	@Override
	public void register(String firstName, String lastName, String userName,
			String userPass, String emailAdd) throws IllegalArgumentException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			
			UserAccount defaultUser = new UserAccount();
			defaultUser.setFirstName(firstName);
			defaultUser.setLastName(lastName);
			defaultUser.setUserName(userName);
			defaultUser.setUserPass(userPass);
			defaultUser.setEmailAdd(emailAdd);
			pm.makePersistent(defaultUser);
		} 
		catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}	
		
	}

	@Override
	public UserInfoDTO getUser(String userName) throws IllegalArgumentException {
		
		return UserAccount.toDTO(UserAccount.getDefaultUser(userName));
	}

}
